package com.example.gybicycle.service;

import java.util.Date;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.enums.NumType;
import com.example.gybicycle.common.sql.CrudService;
import com.example.gybicycle.common.util.DataUtil;
import com.example.gybicycle.common.util.NumberUtil;
import com.example.gybicycle.entity.Lock;
import com.example.gybicycle.dao.LockDao;
import com.example.gybicycle.entity.MemberDeposit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class LockService extends CrudService<LockDao, Lock> {
    @Autowired
    private FileService fileService;
    @Override
    public Lock get(String id) {
        Lock table=super.get(id);
        return table;
    }
    public Result save(Lock entity,String loginId){
        int num=0;
        if(entity.getId()==null){
           
            num=dao.insert(entity);
        }else {
           
            num=dao.update(entity);
        }
        if(num>0)
            return Result.success(entity);
        else 
            return Result.fail("保存失败");
    }

    @Override
    public int insert(Lock entity) {
        entity.setId(NumberUtil.getLockNum(entity.getMainId(),entity.getCid()));
        String qrcode=fileService.createLockQRCode(entity);
        entity.setQrcode(qrcode);
        int num=super.insert(entity);
        return  num;
    }

    @Override
    public int update(Lock entity) {
        String qrcode=fileService.createLockQRCode(entity);
        entity.setQrcode(qrcode);
        return super.update(entity);
    }

    @Override
    public int insertByList(List<Lock> list) {
        for(Lock entity:list){
            entity.setId(NumberUtil.getNum(NumType.LOCK));
            String qrcode=fileService.createLockQRCode(entity);
            entity.setQrcode(qrcode);
        }
        return super.insertByList(list);
    }

    public List<Lock> findList(Lock entity){
        List<Lock> list=super.findList(entity);
        for(Lock lock:list){
            Lock lock1=new Lock();
            lock1.setMainId(lock.getMainId());
            lock1.setGrade(2);
            List<Lock> child=dao.findList(lock1);
            lock.setChildren(child);
        }
        return list;
    }
    public int deleteByMain(Lock entity){
        if(entity.getMainId()!=null&&entity.getMainId().length()>0)
            return dao.deleteByMain(entity);
        else return 0;
    }

    public String findPointName(String id){
        id=id.substring(0,17);
        return dao.findPointName(id);
    }

}